//
//  TWCacheHelper.m
//  TWHomework-WeChat
//
//  Created by wzb on 2019/11/13.
//  Copyright © 2019 WZB. All rights reserved.
//

#import "TWCacheHelper.h"

@implementation TWCacheHelper

+ (void)cacheKeyValues:(NSDictionary *)value forKey:(NSString *)key {
    value = [TWCacheHelper cleanNullInJsonDic:value];
    [[NSUserDefaults standardUserDefaults] setObject:value forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSDictionary *)cleanNullInJsonDic:(NSDictionary *)dic {
    if (!dic || (id) dic == [NSNull null]) {
        return dic;
    }
    NSMutableDictionary *mulDic = [[NSMutableDictionary alloc] init];
    for (NSString *key in [dic allKeys]) {
        NSObject *obj = dic[key];
        if (!obj || obj == [NSNull null]) {
            //            [mulDic setObject:[@"" JSONValue] forKey:key];
        } else if ([obj isKindOfClass:[NSDictionary class]]) {
            [mulDic setObject:[self cleanNullInJsonDic:(NSDictionary *) obj] forKey:key];
        } else if ([obj isKindOfClass:[NSArray class]]) {
            NSArray *array = [TWCacheHelper cleanNullInJsonArray:(NSArray *) obj];
            [mulDic setObject:array forKey:key];
        } else {
            [mulDic setObject:obj forKey:key];
        }
    }
    return mulDic;
}


+ (NSArray *)cleanNullInJsonArray:(NSArray *)array {
    if (!array || (id) array == [NSNull null]) {
        return array;
    }
    NSMutableArray *mulArray = [[NSMutableArray alloc] init];
    for (NSObject *obj in array) {
        if (!obj || obj == [NSNull null]) {
            //            [mulArray addObject:[@"" JSONValue]];
        } else if ([obj isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dic = [self cleanNullInJsonDic:(NSDictionary *) obj];
            [mulArray addObject:dic];
        } else if ([obj isKindOfClass:[NSArray class]]) {
            NSArray *a = [TWCacheHelper cleanNullInJsonArray:(NSArray *) obj];
            [mulArray addObject:a];
        } else {
            [mulArray addObject:obj];
        }
    }
    return mulArray;
}

/**
 *  加入缓存 arr
 */
+ (void)cacheArray:(NSArray *)array forKey:(NSString *)key {
    [[NSUserDefaults standardUserDefaults] setObject:array forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

/**
 *  取出key对应的缓存
 */
+ (NSArray *)getArray:(NSString *)key {
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    return [prefs objectForKey:key];
}

+ (NSDictionary *)getValuesForKey:(NSString *)key {
    return [[NSUserDefaults standardUserDefaults] objectForKey:key];
}


+ (void)cacheString:(NSString *)value forKey:(NSString *)key {
    [[NSUserDefaults standardUserDefaults] setObject:value forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)getStringForKey:(NSString *)key {
    return [[NSUserDefaults standardUserDefaults] stringForKey:key];
}

+ (void)removeCacheForKey:(NSString *)key {
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSDictionary *)getCacheDictionaryForKey:(NSString *)key {
    return [[NSUserDefaults standardUserDefaults] objectForKey:key];
}

@end
